const express = require("express");
const User = require("../db/models/user");
const Admin = require("../db/models/admin");
const bcrypt = require("bcrypt");
const token = require("../middleware/jwt");
const Package = require("../db/models/package");
const Station = require("../db/models/station");
const Delivery = require("../db/models/delivery");
module.exports = {
  async test(req, res) {
    const { id } = req.body;
    Admin.findOne({
      where: {
        id: Number(id),
      },
    }).then((user) => {
      res.json({
        code: 200,
        message: "查询成功",
        data: user,
      });
    });
  },

  // 微信注册登录
  async phoneLogin(req, res) {
    const { phone } = req.body;
    if (!phone) {
      return res.json({
        code: 400,
        message: "参数错误",
        data: null,
      });
    }

    try {
      const user = await User.findOne({
        where: {
          phone: phone,
        },
      });

      if (user) {
        // 生成token
        const tokenStr = token.getTokenUser(user.id, user.phone);
        return res.json({
          code: 200,
          message: "登录成功",
          data: user,
          token: tokenStr,
        });
      } else {
        // 注册
        const newUser = await User.create({
          phone: phone,
        });
        // 生成token
        const tokenStr = token.getTokenUser(newUser.id, newUser.phone);
        return res.json({
          code: 200,
          message: "注册成功",
          data: newUser,
          token: tokenStr,
        });
      }
    } catch (err) {
      return res.status(500).json({
        code: 500,
        message: "服务器错误",
        error: err,
      });
    }
  },
  // 查询用户下的所有包裹
  async getAllPackage(req, res) {
    // 从中间件获取用户id
    // const userId = req.userId
    const phone = req.phone;

    // 提取手机号前三位和后四位
    const phone_number_front = phone.substring(0, 3);
    const phone_number_after = phone.substring(7, 11);
    // 查询包裹表
    Package.findAll({
      where: {
        phone_number_front,
        phone_number_after,
        status: "未取件",
      },
      include: [
        {
          model: Station,
          as: "stations", // 确保与关联关系中的别名一致
        },
        {
          model: Delivery,
          as: "deliverys",
        },
      ],
    }).then((data) => {
      res.json({
        code: 200,
        message: "查询成功",
        data: data,
      });
    });
    // res.json({
    //     code: 200,
    //     message: '失败',
    //     data: []

    // })
  },
  // 用户快递取件
  async takePackage(req, res) {
    const { id } = req.body;
    const userId = req.userId;
    const phone = req.phone;
    // 分割前三位和后四位
    const phone_number_front = phone.substring(0, 3);
    const phone_number_after = phone.substring(7, 11);
    // 查询包裹表
    Package.findOne({
      where: {
        phone_number_front: phone_number_front,
        phone_number_after: phone_number_after,
        status: "未取件",
        delivery_id: Number(id),
      },
    }).then((data) => {
      if (data) {
        // 更新包裹表
        Package.update(
          {
            status: "已取件",
            userId,
          },
          {
            where: {
              delivery_id: Number(id),
            },
          }
        ).then((data) => {
          res.json({
            code: 200,
            message: "取件成功",
            data: data,
            phone_number_after,
            phone_number_front,
          });
        });
      } else {
        res.json({
          code: 400,
          message: "取件失败",
          data: null,
        });
      }
    });
  },
};
